package com.example.fw;

import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;

import com.example.tests.ContactData;
import com.example.utils.SortedListOf;

public class ContactHelper extends HelperBase {

	public ContactHelper(AppManager manager) {
		super(manager);
	}
	
	public ContactHelper createContact(ContactData contact) {
		manager.navigateTo().contactCreationPage();
		fillAddNewContactForms(contact);
		submitContactCreation();
		manager.navigateTo().startPage();
		rebuildCache();
		return this;
	}
	
	public ContactHelper modifyContact(int contactIndex, ContactData contact) {
		manager.navigateTo().startPage();
		initContactEditFromMainPage(contactIndex);
		fillEditForms(contact);
		submitContactModification();
		manager.navigateTo().startPage();
		rebuildCache();
		return this;
	}
	
	public ContactHelper removeContact(int contactIndex) {
		manager.navigateTo().startPage();
		initContactEditFromMainPage(contactIndex);
		deleteContact();
		manager.navigateTo().startPage();
		rebuildCache();
		return this;
		
	}
	
	public SortedListOf<ContactData> getContacts() {	
		if (cachedContacts == null) {
			rebuildCache();
		}
		return cachedContacts;
	}

	// -------------------------------------------------------------------------------------------------------------------------	
	
	private void rebuildCache() {
		manager.navigateTo().startPage();
		cachedContacts = new SortedListOf<ContactData>();
		List<WebElement> entries = driver.findElements(By.xpath("//table[@id='maintable']/tbody/tr[*]"));
		for (int i = 2; i < entries.size(); i++) {
			ContactData contact = new ContactData();
			String firstname = (driver.findElement(By.xpath("//table[@id='maintable']/tbody/tr[" + i + "]/td[2]"))).getText();
			String lastname = (driver.findElement(By.xpath("//table[@id='maintable']/tbody/tr[" + i + "]/td[3]"))).getText();
			String email = (driver.findElement(By.xpath("//table[@id='maintable']/tbody/tr[" + i + "]/td[4]"))).getText();
			String mobilephone = (driver.findElement(By.xpath("//table[@id='maintable']/tbody/tr[" + i + "]/td[5]"))).getText();
			cachedContacts.add(contact.withFirstname(firstname)
							 	 .withLastname(lastname)
							 	 .withEmail(email)
							 	 .withMobilePhone(mobilephone));
		}		
	}

	private SortedListOf<ContactData> cachedContacts;
	
	public ContactHelper submitContactCreation() {
		click(By.name("submit"));
		cachedContacts = null;
		return this;
	} 
	
	public ContactHelper fillAddNewContactForms(ContactData newContactData) {
		fillContactForms(newContactData);
	    changeComboBoxValue(By.name("new_group"), newContactData.getGroup());
	    return this;
	}
	
	public ContactHelper fillEditForms(ContactData editContactData) {
		fillContactForms(editContactData);	
		return this;
	}
	
	public ContactHelper initContactEditFromMainPage(int index) {
		manager.navigateTo().startPage();
		driver.findElement(By.xpath("(//img[@alt='Edit'])[" + (index + 1) + "]")).click();
		return this;
	}
	
	public ContactHelper initContactEditFromDetailsPage(int index) {
		openContactDetails(index);
		driver.findElement(By.name("modifiy")).click();
		return this;
	}

	public ContactHelper openContactDetails(int index) {
		manager.navigateTo().startPage();
		driver.findElement(By.xpath("(//img[@alt='Details'])[" + (index + 1) + "]")).click();
		return this;
	}
	
	public ContactHelper submitContactModification() {
		driver.findElement(By.name("update")).click();
		cachedContacts = null;
		return this;
	}
	
	public ContactHelper deleteContact() {
		driver.findElement(By.xpath("(//input[@name='update'])[2]")).click();
		cachedContacts = null;
		return this;
	}
	
	private ContactHelper fillContactForms(ContactData contactData) {
	    type(By.name("firstname"), contactData.getFirstname());
	    type(By.name("lastname"), contactData.getLastname());
	    type(By.name("address"), contactData.getHomeAddressData().getAddress());
	    type(By.name("home"), contactData.getHomeAddressData().getPhone());
	    type(By.name("mobile"), contactData.getMobilephone());
	    type(By.name("work"), contactData.getWorkphone());
	    type(By.name("email"), contactData.getEmail());
	    type(By.name("email2"), contactData.getEmail2());
	    changeComboBoxValue(By.name("bday"), contactData.getBirthdayDate().getBday());
	    changeComboBoxValue(By.name("bmonth"), contactData.getBirthdayDate().getBmonth());
	    type(By.name("byear"), contactData.getBirthdayDate().getByear());
	    type(By.name("address2"), contactData.getSecondAddressData().getAddress());
	    type(By.name("phone2"), contactData.getSecondAddressData().getPhone());	
	    return this;
	}

}
